package callnumber.gtdev5.com.photoVault.service;

import android.annotation.SuppressLint;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.junruy.fakecall.KeepLiveAidlInterface;

import callnumber.gtdev5.com.photoVault.utils.ServiceCheckUtils;


/********************
 *  Created by ：  Zeoy 
 *         Date：  2018\3\29 0029.
 * Introduction：   jobhandlerservice 保活服务
 *********************/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class JobHandlerService extends JobService {
    private JobScheduler mJobScheduler;
    private Intent intent ;
    private Binder binder;
    private ServiceConnection connection;


    @SuppressLint("WrongConstant")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        JobInfo.Builder builder = new JobInfo.Builder(startId++,
                new ComponentName(getPackageName(), JobHandlerService.class.getName()));

        builder.setPeriodic(5000); //每隔5秒运行一次
        builder.setRequiresCharging(true);
        builder.setPersisted(true);  //设置设备重启后，是否重新执行任务
        builder.setRequiresDeviceIdle(true);

        if(!ServiceCheckUtils.isServiceWorked(this, "callnumber.gtdev5.com.fakecall_4.service.NotificationListenerService")){
            Log.e("zeoy", "【      jobservice 启动后台】");
            startService(new Intent(JobHandlerService.this,NotificationListenerService.class));
        }
        bindService(intent,connection,BIND_IMPORTANT);


        if (mJobScheduler.schedule(builder.build()) <= 0) {
            //If something goes wrong
            System.out.println("工作失败");
        } else {
            System.out.println("工作成功");
        }
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        intent = new Intent(this,NotificationListenerService.class);
        binder = new KeepLiveAidlInterface.Stub.Stub() {
            @Override
            public String getServiceName() throws RemoteException {
                return "KeepAccessibilityService";
            }
        };
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                startService(intent);
                bindService(intent,connection,BIND_ADJUST_WITH_ACTIVITY);
            }
        };
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.e("zeoy", "【      jobservice 停止】");
        if(!ServiceCheckUtils.isServiceWorked(this, "callnumber.gtdev5.com.fakecall_4.service.NotificationListenerService")){
            Log.e("zeoy", "【      jobservice 重新启动后台】");
            startService(new Intent(JobHandlerService.this,NotificationListenerService.class));
        }

        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        startService(new Intent(getApplicationContext(),JobHandlerService.class));
    }
}